#!/usr/bin/ruby
require 'rubygems'
require 'dbaccess.rb'
require 'standardquote.rb'
require 'historicalquote.rb'
require 'perf.rb'
require 'xmlparse.rb'

unless (ARGV.length >= 1)
  puts "Usage: run.rb command <parameters>"
end

cmd = ARGV[0]

x = XmlParse.new("config.xml")
p = x.getparams()
hostname = p["hostname"]
user = p["user"]
pass = p["pass"]
dbname = p["dbname"]

if (cmd == "init")
  dbobj = DbAccess.new(hostname, user, pass, dbname)
  puts "Initializing databases..."
  dbobj.createnew()
  puts "Done."
end

if (cmd == "loadsymbols")
  dbobj = DbAccess.new(hostname, user, pass, dbname)
  puts "Loading symbols..."
  dbobj.loadsymbols("../data/NASDAQ.txt", "NASDAQ")
  dbobj.loadsymbols("../data/NYSE.txt", "NYSE")
  puts "Done."
end

if (cmd == "loadhistorical")
  unless (ARGV.length == 3)
    puts "Usage: run.rb loadhistorical exchange MM-DD-YYYY"
    Process.exit
  end
  exchange = ARGV[1]
  sd = Date.parse(ARGV[2])
  quotes = HistoricalQuote.new(hostname, user, pass, dbname)
  puts "Loading historical data for exchange #{exchange}..."
  quotes.loadhistoricaldata(sd.to_s, exchange)
end

if (cmd == "loadstandard")
  unless (ARGV.length == 2)
    puts "Usage: run.rb loadstandard exchange"
    Process.exit
  end
  exchange = ARGV[1]
  quotes = StandardQuote.new(hostname, user, pass, dbname)
  puts "Loading Standard quotes for exchange #{exchange}..."
  quotes.loadexchange(exchange)
  quotes.close()

end

if (cmd == "perf")
  unless (ARGV.length == 2)
    puts "Usage: run.rb perf exchange"
    Process.exit
  end
  exchange = ARGV[1]
  p = Perf.new(hostname, user, pass, dbname)
  puts "Computing performance for exchange #{exchange}..."
  p.generateperf(exchange)
  p.close()
  
end
